package cn.how2go.net.server.protocol;

import cn.how2go.net.server.util.BinaryUtil;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.util.Arrays;

/**
 * Created by Administrator on 2018/4/19.
 */
public class JTBytes {
    private int headerSize;
    private JTHeader header;
    private byte[] body;
    private byte crc;
    private byte prefix;
    private byte endfix;

    public JTHeader getHeader() {
        return header;
    }

    public void setHeader(JTHeader header) {
        this.header = header;
    }

    public byte[] getBody() {
        return body;
    }

    public void setBody(byte[] body) {
        this.body = body;
    }

    public byte getCrc() {
        return crc;
    }

    public void setCrc(byte crc) {
        this.crc = crc;
    }

    public byte getPrefix() {
        return prefix;
    }

    public void setPrefix(byte prefix) {
        this.prefix = prefix;
    }

    public int getHeaderSize() {
        return headerSize;
    }

    public void setHeaderSize(int headerSize) {
        this.headerSize = headerSize;
    }

    public byte getEndfix() {
        return endfix;
    }

    public void setEndfix(byte endfix) {
        this.endfix = endfix;
    }

    public byte[] write(){
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeByte(prefix);
        directBuf.writeBytes(header.write());
        directBuf.writeBytes(body);
        directBuf.writeByte(crc);
        directBuf.writeByte(prefix);
        byte[] message = new byte[directBuf.readableBytes()];
        directBuf.readBytes(message);
        directBuf.release();
        //转义字符
        return message;
    }

    public byte[] writeBody(){
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeBytes(header.write());
        directBuf.writeBytes(body);
        byte[] message = new byte[directBuf.readableBytes()];
        directBuf.readBytes(message);
        directBuf.release();
        return message;
    }

    public void read(byte[] bytes){
        ByteBuf directBuf = Unpooled.directBuffer(16);
        directBuf.writeBytes(bytes);
        prefix = directBuf.readByte();
        short msgtype = directBuf.readShort();
        short message = directBuf.readShort();
        byte[] uid = new byte[6];
        directBuf.readBytes(uid);
        String simno = BinaryUtil.bcd2Str(uid);
        short seriaNo = directBuf.readShort();
        boolean packages = isPackage(message);
        boolean rsa = isRsa(message);
        int msgLength = getLength(message);
        headerSize = packages?16:12;
        header = new JTHeader();
        header.setMsgType(msgtype);
        header.setMessage(message);
        header.setLength((short) msgLength);
        header.setPackages(packages);
        header.setRsa(rsa);
        header.setSimno(simno);
        header.setSeriaNo(seriaNo);
        if(packages){
            short allpackage = directBuf.readShort();
            short packageno = directBuf.readShort();
            header.setAllpackage(allpackage);
            header.setPackageno(packageno);
        }

        byte[] bodyBytes = new byte[msgLength];
        directBuf.readBytes(bodyBytes);
        body = bodyBytes;

        crc = directBuf.readByte();
        endfix = directBuf.readByte();
        directBuf.release();
    }

    private boolean isPackage(short message){
        boolean flag = false;
        if((message & 0x2000) == 0x2000){
            flag = true;
        }
        return flag;
    }

    private boolean isRsa(short message){
        boolean flag = false;
        if((message & 0x400) == 0x400){
            flag = true;
        }
        return flag;
    }

    public int getLength(short message){
        return message & 0x03ff;
    }

    @Override
    public String toString() {
        return "JTBytes{" +
                "headerSize=" + headerSize +
                ", header=" + header +
                ", body=" + Arrays.toString(body) +
                ", crc=" + crc +
                ", prefix=" + prefix +
                ", endfix=" + endfix +
                '}';
    }
}
